package pe.com.redhut.karaosys.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import pe.com.redhut.karaosys.bean.Cliente;
import pe.com.redhut.karaosys.bean.Pedido;
import pe.com.redhut.karaosys.bean.Sala;
import pe.com.redhut.karaosys.ex.DAOException;
import pe.com.redhut.karaosys.util.ConexionDB;
import pe.com.redhut.karaosys.util.Filtro;

public class PedidoDAO extends BaseDAO {
	
	public Pedido buscar(Sala sala, Filtro filtro) throws DAOException {
		
		Pedido pedido = new Pedido();
		PreparedStatement pstm = null;
		Connection conn = null;
		String sql = "select a.codped, a.codsal, b.nombre as sala, a.nromesa, isnull(a.codcli) as codcli, " +
				" isnull(c.nombre + ' ' + c.apepat) as cliente, a.montototal, a.fecha, a.hora " +
				"from pedido " +
				"where codsal = ? " +
				"and nromesa = ?";
		
		try {
			conn = ConexionDB.getConnection();
			
			pstm = conn.prepareStatement(sql);
			pstm.setString(1,sala.getCodigo());
			pstm.setString(2,filtro.getMesa());
			
			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				pedido.setNumero(rs.getString("codped"));
				pedido.setSala(new Sala(rs.getString("codsal"), rs.getString("sala")));
				pedido.setCliente(new Cliente(rs.getString("codcli"),rs.getString("apepat"),rs.getString("nombre")));
				pedido.setNumeroMesa(rs.getString("nromesa"));
				pedido.setMontoTotal(rs.getDouble("montototal"));
				pedido.setFecha(rs.getDate("fecha"));
				pedido.setHora(rs.getString("hora"));
				
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		} finally {
			this.closeStatement(pstm);
			this.closeConnection(conn);
			
		}
		
		return pedido;
	}
	
	public String insertar (Pedido pedido)  throws DAOException {
		String rs = "";
		
		return rs;
	}
	
}
